package com.etc.MapReduce;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

public class Mapreduce {
    public static class TestMap extends Mapper<LongWritable, Text,Text, IntWritable> {

        String fileName = "";
        Text k = new Text();

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {
            //获取文件名
            FileSplit split = (FileSplit) context.getInputSplit();
            fileName = split.getPath().getName();
        }

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            //文本内容强制转换
            String line = value.toString();
            //按照空格切分
            String[] Array = line.split(" ");
            for (String s : Array) {
                k.set(s + "-" + fileName);
                context.write(k, new IntWritable(1));
            }
        }
    }
        public static class TestReduce extends Reducer<Text, IntWritable,Text,IntWritable> {
            @Override
            protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
                int line = 0;
                for (IntWritable value : values) {
                    line += value.get();
                }
                context.write(new Text(key), new IntWritable(line));
            }
        }

        public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {


            Configuration conf = new Configuration();
            //创建一个mapreduce的工作
            Job job = Job.getInstance(conf);
            job.setJarByClass(Mapreduce.class);
            //map的class在哪里
            job.setMapperClass(Mapreduce.TestMap.class);
            //reduce的calss在哪里
            job.setReducerClass(Mapreduce.TestReduce.class);
            //map的输出类型
            job.setMapOutputKeyClass(Text.class);
            job.setMapOutputValueClass(IntWritable.class);
            //reduce的输出类型
            job.setOutputKeyClass(Text.class);
            job.setOutputValueClass(IntWritable.class);
            //判断文件是否存在，存在则删除
            File file = new File("f:\\output");
            if (file.exists()) {
                FileUtils.deleteDirectory(file);
            }
            //计算的文件在哪里
            FileInputFormat.setInputPaths(job, new Path("f:\\intput"));
            FileOutputFormat.setOutputPath(job, new Path("f:\\output"));

            job.waitForCompletion(true);
            }
        }

